alter PROCEDURE [dbo].[PrintDistSit] @Production_line NVARCHAR(50),
                                     @PageIndex INT,
                                     @PageSize INT,
                                     @TotalCount INT OUTPUT,
    --站点
                                     @site NVARCHAR(50),
    --工单状态
                                     @BoStatus NVARCHAR(50),
    --批量工单号
                                     @batch_number_list VARCHAR(2000)
AS
BEGIN
    SET NOCOUNT ON;

    -- 计算分页的起始位置
    DECLARE @Offset INT = (@PageIndex - 1) * @PageSize;


    select a.batch_number,
           a.production_line,
           a.team,
           a.planner_code,
           --2025-1-16修改
           d.name                                              as MES订单类型,
           a.current_site,
           -- 2025-1-16修改 站点扫描表最新时间
           g.gmt_create                                           站点扫描时间,
           c.name                                              as status,
           -- 2025-1-16修改 产品名称
           a.item_code                                         as 产品名称,
           a.number_of_layouts,
           a.qty,
           a.Unit,
           a.process_line_desc,
           a.mes_order_create_time                             as gmt_create,
           a.standard_shipping_date,
           -- 2025-1-16修改 暂时无需修改
--            (select top 1 resource_name from mes_ps_print where b.bo_id = a.id) as resource_type,
           a.customer_code,
           a.customer_name,
           a.wh_org,
           a.item_category,
           a.typesetting_method,
           --新增字段 扫码备注 mes_site_scan.scan_remark 最新
           g.scan_remark                                       as scan_remark,
           --新增字段 开纸信息 工序大料为物料的
           (select top 1 open_paper_info
            from mes_bo_process_material
            where bo_process_id =
                  (select top 1 id from mes_bo_process where bo_id = a.id and group_type = 'cutting'))
                                                               as open_paper_info,
           --  新增字段 纸张开数
           (select top 1 mes_bo_process_material.paper_open
            from mes_bo_process_material
            where bo_process_id =
                  (select top 1 id from mes_bo_process where bo_id = a.id and group_type = 'cutting'))
                                                               as paper_open,
           -- 新增字段 总放数
           a.gross_discharge_qty,
           --2025-1-16修改 净需求数
           a.paper_used_num_total                              as 净需求数,
           --2025-1-16修改  新增字段 客户要求出货时间
           a.customer_requires_shipping_date                   as 客户要求出货时间,
           --2025-1-16修改 新增字段 ppc复期
           a.ppc_recurrence                                    as ppc复期,
-- 备料物料编码 2025-1-16修改
           (select top 1 mes_bo_process_material.material_code
            from mes_bo_process_material
            where bo_process_id =
                  (select top 1 id
                   from mes_bo_process
                   where bo_id = a.id
                     and group_type = 'cutting'))              as 备料物料编码,
-- 备料物料类型 2025-1-16修改
           (select name
            from sys_material_type
            where id = (select top 1 mes_bo_process_material.material_type
                        from mes_bo_process_material
                        where bo_process_id =
                              (select top 1 id
                               from mes_bo_process
                               where bo_id = a.id
                                 and group_type = 'cutting'))) as 备料物料类型,

-- lot个数
           a.lot_qty                                           as lot个数,
           f.name                                              as 站点归属,
           a.printer_type                                      as 'printer_type'


    from mes_batch_order a
             left join mes_mr_doc_detail b on a.id = b.bo_id
             left join sys_dict c on c.value = a.status and c.type = 'ORDER_STATUS'
             left join sys_dict d on d.value = a.mes_order_type and d.type = 'MES_ORDER_TYPE'
             left join mes_site e on a.current_site_id = e.id
             left join sys_dict f on CAST(f.id as varchar) COLLATE Chinese_PRC_CI_AI =
                                     CAST(e.site_attribution_id as varchar) COLLATE Chinese_PRC_CI_AI
             outer apply (select top 1* from mes_site_scan g where g.bo_id = a.id order by g.gmt_create desc) g


-------------------------------------------------------------------


    where (@Production_line IS NULL OR a.production_line = @Production_line)
      and (@site IS NULL OR a.site_code in (SELECT value FROM STRING_SPLIT(@site, ',')))
      AND (@BoStatus IS NULL OR a.status in (SELECT value FROM STRING_SPLIT(@BoStatus, ',')))
      AND (@batch_number_list IS NULL OR a.batch_number IN (SELECT value FROM STRING_SPLIT(@batch_number_list, ' ')))

    order by a.gmt_create desc


    OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY;

    SELECT @TotalCount = COUNT(1)
    from mes_batch_order a
             left join mes_mr_doc_detail b on a.id = b.bo_id
             left join sys_dict c on c.value = a.status and c.type = 'ORDER_STATUS'
             left join sys_dict d on d.value = a.mes_order_type and d.type = 'MES_ORDER_TYPE'
             left join mes_site e on a.current_site_id = e.id
             left join sys_dict f on CAST(f.id as varchar) COLLATE Chinese_PRC_CI_AI =
                                     CAST(e.site_attribution_id as varchar) COLLATE Chinese_PRC_CI_AI
             outer apply (select top 1* from mes_site_scan g where g.bo_id = a.id order by g.gmt_create desc) g

    -------------------------------------------------------------------------------------------------
    where (@Production_line IS NULL OR a.production_line = @Production_line)
      and (@site IS NULL OR a.site_code in (SELECT value FROM STRING_SPLIT(@site, ',')))
      AND (@BoStatus IS NULL OR a.status in (SELECT value FROM STRING_SPLIT(@BoStatus, ',')))
      AND (@batch_number_list IS NULL OR a.batch_number IN (SELECT value FROM STRING_SPLIT(@batch_number_list, ' ')))


END